import React, {Component, PropTypes} from 'react';
import Signals from 'signals';
import MenuTreeContainer from '../Tree/MenuTreeContainer';
import ShowEditTableComponent from '../Table/ShowEditTableComponent';
import ShowListContainer from '../List/ShowListContainer';
import {getTableList, getTableInfo} from "../../services/showServer";
import {getSubjectContent} from "../../services/showServer";

import styles from './Main.less';

class ShowMainComponent extends React.Component {
  constructor(props) {
    super(props);
    //signals事件
    this.signal = new Signals.Signal();
    this.listSignal = new Signals.Signal();
    this.commentSignal = new Signals.Signal();
    this.state = {};
  }

  componentWillMount() {
    this.signal.add(function (point) {
      if (point.type == 1) {
        getSubjectContent(point.id, "tables").then(function (json) {
          this.refs.list.setState({data: json});
        }.bind(this));
      } else {
        getTableList(point.id).then(function (json) {
          this.refs.list.setState({data: json});
        }.bind(this));
      }
    }.bind(this));

    this.listSignal.add(function (info) {
      getTableInfo(info).then(function (json) {
        var meta = JSON.parse(json.tableMeta);
        var explorationMeta = [];
        if (json.explorationMeta != null && json.explorationMeta != '') {
          explorationMeta = JSON.parse(json.explorationMeta);
        }

        var pColumns = [];
        var columns = [];
        if (meta.partitioned == 'true') {
          pColumns = meta.partitionColumns;
        }
        if (meta.columns != '') {
          columns = meta.columns;
        }


        this.refs.table.setState({
          remark: json.remark,
          editComment: json.comment,
          comment: json.comment,
          partitionColumns: pColumns,
          columnsFilter: columns,
          columns: columns,
          tableMeta: meta,
          explorationMeta: explorationMeta,
          table: json,
        });

      }.bind(this));
    }.bind(this));

    this.commentSignal.add(function (info) {
      this.refs.list.updateData(info);
    }.bind(this));
  }

  componentWillUnmount() {
    this.signal.removeAll();
    this.listSignal.removeAll();
    this.commentSignal.removeAll();
  }

  render() {
    return (
      <div className={styles.content}>
        <div className={styles.show_lside}>
          <MenuTreeContainer signal={this.signal} ref="tree"/>
        </div>
        <div className={styles.show_rside}>
          <ShowListContainer listSignal={this.listSignal} ref="list"/>
        </div>
        <div className={styles.show_main}>
          <ShowEditTableComponent commentSignal={this.commentSignal} listSignal={this.listSignal} ref="table"/>
        </div>
      </div>
    );
  }
}

export default ShowMainComponent;
